﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SimpleLibrary
{
    public class QueryStatistics
    {
        private static Queue<QueryStatistics> myQueue = new Queue<QueryStatistics>();
        private string task;
        private string strQuery;
        private double executionTime;

        // Insert Query Execution information
        public void Insert(string task,string strQuery,double executionTime){
            lock (myQueue)
            {
                QueryStatistics stats = new QueryStatistics();
                stats.task = task;
                stats.strQuery = strQuery;
                stats.executionTime = executionTime;
                myQueue.Enqueue(stats);
            }
        }

        // Gather statistics information and Generate result HTML file.
        public static void GenerateReport(DateTime mydate)
        {
            var status =
                from queue in myQueue
                where queue != null
                group queue by new { queue.task, queue.strQuery} into querygroup
                orderby querygroup.Key.task, querygroup.Key.strQuery
                select new { Name = querygroup.Key, Count = querygroup.Count(), ResponseTime = querygroup.Average(q => q.executionTime), Max = querygroup.Max(q=>q.executionTime), Min = querygroup.Min(q=>q.executionTime) };

            StringBuilder sb = new StringBuilder();
            sb.Append("<html>");
            sb.Append("<head>");
            sb.Append("<style type='text/css'>");
            sb.Append(" table.example2 {background-color:transparent;border-collapse:collapse;width:100%;} ");
            sb.Append(" table.example2 th, table.example2 td {text-align:center;border:1px solid black;padding:5px;} ");
            sb.Append(" table.example2 th {background-color:AntiqueWhite;} ");
            sb.Append(" table.example2 td:first-child {width:20%;} ");
            sb.Append("</style> ");
            sb.Append("</head>");
            sb.Append("<body>");
            sb.Append("<h1>SimpleLoadGenertor Report</h1>");
            sb.Append("<p>");
            sb.Append("<h2>Application Configuration</h2>");
            sb.Append("Number Of workers:" + SqlLoadGen.numberOfWorker + "<br>");
            sb.Append("Timeout:" + SqlLoadGen.timeOut + "sec <br>");
            sb.Append("<h2>Query Execution Result</h2> <br>");
            sb.Append("<table class='example2'>");
            sb.Append("<tr>");
            sb.Append("<th> Task/Query </th>");
            sb.Append("<th> Count </th>");
            sb.Append("<th> Average Response Time(ms) </th>");
            sb.Append("<th> Maximum Response Time(ms) </th>");
            sb.Append("<th> Minimum Response Time(ms) </th>");
            sb.Append("</tr>");

            foreach (var stats in status)
            {
                sb.Append("<tr>");
                sb.Append("<td width='50%'>" + stats.Name + "</td>");
                sb.Append("<td>" + stats.Count + "</td>");
                sb.Append("<td>" + stats.ResponseTime + "</td>");
                sb.Append("<td>" + stats.Max + "</td>");
                sb.Append("<td>" + stats.Min + "</td>");
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            if (ExceptionStatistics.GetExceptionCount() > 0)
            {
                sb.Append("<br> There were " + ExceptionStatistics.GetExceptionCount() + " Errors during the execution <br>");
                sb.Append("<a href='Error" + mydate.ToShortDateString() + ".html'>click Here to see the details </a>");
            }

            sb.Append("</body>");
            sb.Append("</html>");


            using (FileStream fs = new FileStream("Result" + mydate.ToShortDateString() + ".html", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine(sb.ToString());
                }
            }

            System.Diagnostics.Process.Start("Result" + mydate.ToShortDateString() + ".html");

        }
    }

    public class TempStatistics
    {
        public string strTaskName;
        public string strCommandText;
        public double iExecutionTime;
    }
}
